<template>
	<CenterBox>
    <div class="logo">
      <img />
      <div style="font-size: 28px;text-align: left;margin-bottom: 20px;">
        <span style="color: #0091FF">C</span>
        <span>loud</span>
        <span style="color: #0091FF">D</span>
        <span>esk</span>
      </div>
    </div>
    <el-collapse-transition>
      <div class="username" v-show="tab=='username'">
        <div class="signin-head">
          <div class="title">登录</div>
          <div class="subtitle">Sign In</div>
        </div>
        <div>
          <input ref="username" type="email" @keyup.enter="next" @focus="errMsg = ''" v-model="userVO.username" placeholder="邮箱/用户名" class="zsh-input" />
        </div>
        <div class="error" v-show="errMsg!=''">
          {{errMsg}}
        </div>
        <ZshButton :loading="loading" name="next" @click="next">下一步</ZshButton>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div class="password" v-show="tab=='password'">
        <div class="signin-head">
          <div class="title">输入密码</div>
          <div class="subtitle">Password</div>
        </div>
        <div style="text-align: left;">
          <div class="username-box">
            <span>{{userVO.username}}</span>
            <span class="blue" @click="modifyUsername">更改</span>
          </div>
        </div>
        <div>
          <input ref="password" type="password" @keyup.enter="login" @focus="errMsg = ''" v-model="userVO.password" placeholder="请输入密码" class="zsh-input" />
        </div>
        <div class="error" v-show="errMsg!=''">
          {{errMsg}}
        </div>
        <ZshButton :loading="loading" name="login" @click="login">登录</ZshButton>
      </div>
    </el-collapse-transition>
    <div class="forgot">
      <router-link style="font-size:14px;color:#888888" to="forgot">忘记密码?</router-link>
    </div>
    <div class="signup">
      <span style="font-size:13px;color:#888888">还没有账户?<router-link to="signup">立即注册</router-link></span>
    </div>
  </CenterBox>
</template>

<script>
	import {login,getUserInfo,exists} from '@/api/auth/index'
  import ResultCode from '@/config/result.code'
  import {setToken, setLastUserName, getLastUserName, setRefreshToken} from "@/libs/util";
	import {connect} from "@/api/im";
	import {autoRefreshToken} from "@/api/auth/refresh";

  import CenterBox from './components/center-box'
  import ZshButton from './components/zsh-button'

  export default{
		data(){
			return {
				tab: 'username',
				loading: '',
				userVO:{
					username:'',
					password:''
				},
        errMsg:''
			}
		},
    components:{
		  CenterBox,
      ZshButton
    },
    created() {
		  this.check()
    },
    mounted() {
		  let that = this
		  this.$nextTick(function (){
		    let lastUsername = getLastUserName()
        if(lastUsername != null) {
          that.userVO.username = lastUsername
        }else{
          this.$refs.username.focus()
        }
      })
    },
    methods:{
		  check(){
        getUserInfo().then(res => {
		      if(res && res.code == 200){
            this.$store.state.userInfo = res.data
		        this.toTargetPage()
          }
        })
      },
      toTargetPage(){
		    let target = this.$route.query['target']
        this.$router.replace({path:target?target:'/'})
      },
			next(){
				var that = this
        if(this.userVO.username.trim().length == 0) {
          return
        }
        if(this.loading == 'next') {
          return
        }
        this.errMsg = ''
				this.loading = 'next'
        exists({username:this.userVO.username}).then(res => {
          if(res.code == 200) {
            this.tab = 'password'
            setTimeout(function (){
              that.$refs.password.focus()
            },1)
          }else{
            this.errMsg = '用户不存在，请检查输入'
          }
        }).catch((reason => {

        })).finally(()=>{
          that.loading = ''
        })
			},
			login(){
		    if(this.userVO.password.trim().length == 0) {
          this.errMsg = '请输入密码'
		      return
        }
        if(this.loading == 'login') {
          return
        }
        this.errMsg = ''
				this.loading = 'login'
				login(this.userVO).then(res => {
				  if(res.code == 200){
				    console.log(res.data)
            setToken(res.data.accessToken)
            setRefreshToken(res.data.refreshToken)
            autoRefreshToken()
            setLastUserName(this.userVO.username)
            this.$store.commit('updateUserInfo',res.data)
            this.toTargetPage()
          }else{
            this.errMsg = '密码错误，请重新输入'
          }
        }).catch((reason => {
          this.$message.error('请检查网络连接')
        })).finally(()=>{
          this.loading = ''
        })
			},
			modifyUsername(){
				this.tab = 'username'
				this.userVO.password = ''
			}
		}
	}
</script>

<style lang="less" scoped>
  .signin-head{
    margin-bottom: 20px;
    text-align: left;
    .title {
      font-size: 24px;
      font-weight: 500;
      line-height: 30px;
      margin-bottom: 10px;
    }
    .subtitle{
      font-size: 16px;
      line-height: 30px;
    }
  }

  .error {
    text-align: left;
    color: orangered;
    padding-top: 10px;
  }

	.username{
		transition: all .5s;
		-webkit-transition: all .5s;
		-moz-transition: all .5s;
		-o-animation: all .5s;
		overflow: hidden;
    text-align: left;
	}

	.username-box {
		display: inline-flex;
		justify-content: space-between;
		align-items: center;
		border: 1px solid #EEEEEE;
		margin-bottom: 20px;
		border-radius: 7px;
		cursor: pointer;
		box-sizing: border-box;
		max-width: 100%;
		flex-wrap: nowrap;
		span {
			padding: 10px;
		}
	}
	.password{
		-webkit-transition: all .5s;
		-moz-transition: all .5s;
		-o-animation: all .5s;
		overflow: hidden;
    text-align: left;
	}
  .signup {
    margin: 10px 0;
  }
	@media only screen and (max-width: 600px){
		.zsh-input {
			background-color: transparent;
			width: 100%;
			border: none;
			border-bottom: 2px solid #E8E8E8;
			text-indent: 0px;
			font: 400 13.3333px Arial;
			font-size: 16px;
			padding: 12px 1px;
			border-radius: 0;
			background-color: #FFF;
			outline:none;
			&:focus{
				border-bottom: 2px solid #409EFF;
			}
			&::-webkit-input-placeholder{
					color:#A9B2BB;
			}
			&::-moz-placeholder{   /* Mozilla Firefox 19+ */
					color:#A9B2BB;
			}
			&:-moz-placeholder{    /* Mozilla Firefox 4 to 18 */
					color:#A9B2BB;
			}
			&:-ms-input-placeholder{  /* Internet Explorer 10-11 */
					color:#A9B2BB;
			}
		}
	}
	@media only screen and (min-width: 600px){
		.zsh-input {
			display: block;
			width: 100%;
			height: 44px;
			box-sizing: border-box;
			border-radius: 2px;
			text-indent: 12px;
			font-size: 16px;
			outline: none;
			border: none;
			padding-right: 12px;
			transition: all .2s ease-in-out;
			background: #f8f8f8;
			border: 1px solid #E4E4E4;
		}
	}
</style>
